<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');
/**
 * CodeIgniter Application Controller Class
 *
 * This class object is the super class that every library in
 * CodeIgniter will be assigned to.
 *
 * @package        CodeIgniter
 * @subpackage    Libraries
 * @category    Libraries
 * @author        opmetic@gmail.com
 * @link        http://codeigniter.com/user_guide/general/controllers.html
 */
class XX_Controller extends CI_Controller
{

    private static $instance;

    public $user_code; //用户编码
    public $role; //角色对象，包含权限控制
    public $logged_in; // 是否登陆
    public $c; //控制器
    public $m; //方法
    public $u; //url串的base64值，用于在页面跳转时，传递必须数据
    public $view_data; //传给view的数据
    /**
     * Constructor
     */
    public function __construct()
    {
        self::$instance =& $this;

        //初始化属性
        $this->user_code = "";
        $this->role = false; //角色对象，包含权限控制
        $this->logged_in = false;
        $this->c = '';
        $this->m = '';
        $this->u = '';

        $this->view_data = array();

        //调用原有的构造函数
        parent::__construct();

        //加载session类
        $this->load->library('session');

        //加载code类
        $this->load->library('XXCode');

        //test start
        //test end

        //调试用 如果是ajax调用，不显示调试信息
        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
            $this->output->enable_profiler(FALSE);
        } else {
            $this->output->enable_profiler(FALSE);
        }

        $this->load->helper('url');
    }

    /**
     * 返回json串
     * @param $data
     */
    public function outputJson($data, $oflag)
    {
        if ($oflag) {
            $data = json_encode($data);
        }
        $this->output
            ->set_content_type('application/json')
            ->set_output($data);
    }

    /**
     * @param $data
     */
    public function show($data)
    {

        if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
            echo $data;
        } else {
            echo '<!DOCTYPE html>
                <html>
                <head>
                    <meta charset="utf-8"/>
                    <META http-equiv="Content-Type" content="text/html; charset=x-user-defined">
                    <TITLE>xx</TITLE>
                    <META name="viewport" content="width=device-width">
                    <LINK href="/images/admin.css" rel="stylesheet" type="text/css">
                    <link href="/images/xx.menu.css" rel="stylesheet" type="text/css">
                    <LINK href="/images/favicon.ico" rel="icon" type="image/x-icon">
                    <LINK href="/images/touchicon.png" rel="apple-touch-icon">
                    <script type="text/javascript" src="/javascript/jquery-1.8.0.min.js" charset="utf-8"></script>
                </head><body>';
            echo $data;
            echo '</body></html>';
        }
    }

    /**
     * 验证权限
     */
    private function _authentication()
    {
        $code = $this->xxcode->get_url_code($this->c, $this->m);

        //验证权限
        if ($this->role) {
            return $this->role->permission($code);
        }
        return false;
    }

    /**
     * 初始化值
     * @param $rsegment
     */
    public function _init($class, $method)
    {
        //获取session值
        $session = $this->session->all_userdata();

        if (isset($session['logged_in']) && $session['logged_in']) { //已登陆
            $this->user_code = $session['user_code'];
            $this->logged_in = $session['logged_in'];
//            $this->role = $session['role']; //用户解色ID
        }

//        $this->c = $rsegment[1]; //控制器
//        $this->m = $rsegment[2]; //方法
        $this->c = $class; //控制器
        $this->m = $method; //方法

        $class_base = $class = "authentication";

        //
        require(CACHEPATH . 'authentication/' . $class_base . '.php');
        $this->role = new $class();

        if (isset($session['role_code'])) { //有角色
            $role_code_arr = explode(",", $session['role_code']);
            foreach ($role_code_arr as $role_code) {
                $class = $class_base . '_' . $session['role_code'];
                if (file_exists(CACHEPATH . 'authentication/' . $class . '.php')) {
                    require(CACHEPATH . 'authentication/' . $class . '.php');
                    $more_role = new $class();
                    $this->role->whitelist = array_merge($this->role->whitelist, $more_role->whitelist);
                    $this->role->blacklist = array_merge($this->role->blacklist, $more_role->blacklist);
                }
            }
        }
    }

    /**
     * 向页面传递参数
     * @param $k
     * @param $v
     */
    public function assign($k, $v)
    {
        $this->view_data[$k] = $v;
    }

    /**
     * @param $v 必须为数组
     */
    public function assign_u($v)
    {
        $this->load->helper('serialize');

        if (is_array($v)) {
            $v_tmp = array();
            if ($this->u) {
                $v_tmp = xx_decode($this->u);
            }
            $v_tmp = array_merge($v_tmp, $v);
            $this->u = xx_encode($v_tmp);
        }

        $this->assign("u", $this->u);
    }

    /**
     * 重中路由
     */
    public function _remap($method, $params = array())
    {
        //获取控制器和方法名
//        $rsegment = $this->uri->rsegment_array();print_r($this->router);exit;//->method class

        //初始化值
        $this->_init($this->router->class, $this->router->method);

        //验证是否可访问
        if ($this->_authentication() && method_exists($this, $method)) {
            return call_user_func_array(array($this, $method), $params);
        } else {
            //show_404();
            echo "没有权限";
            exit;
            header('location: /');
        }

    }
}
// END Controller class

/* End of file Controller.php */
/* Location: ./system/core/Controller.php */